package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.Role;
import com.itheima.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    public PageInfo<Role> findByPage(Integer pageNum, Integer pageSize, String companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo<Role>(list, 5);
    }

    public Role findById(String id) {
        return roleDao.findById(id);
    }

    public void save(Role role) {
        roleDao.save(role);
    }

    public void update(Role role) {
        roleDao.update(role);
    }

    public void delete(String id) {
        roleDao.delete(id);
    }

    public List<Role> findByUserId(String id) {
        return roleDao.findByUserId(id);
    }

    public void changeRole(String userId, String[] roleIds) {
        roleDao.deleteByUserId(userId);
        for (String roleId : roleIds) {
            roleDao.insertUserRole(userId, roleId);

        }
    }

    public void updateRoleModule(String roleId, String[] moduleIds) {
        roleDao.deleteByRoleId(roleId);
        for (String moduleId : moduleIds) {
            roleDao.insertRoleModule(roleId,moduleId);
        }
    }
}
